<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\Repository;

use App\Model\GoodsCollectModel;
use Hyperf\Di\Annotation\Inject;

class GoodsCollectRepository extends BaseRepository
{
    /**
     * @Inject
     * @var GoodsCollectModel
     */
    protected $model;

    /**
     * Notes: 添加/移除 收藏
     * Author: Bruce.z
     * DateTime: 2022/7/18 9:45
     * @param int $goods_id
     * @param int $member_id
     * @return bool
     */
    public function collect(int $goods_id, int $member_id):bool
    {
        $_data = ['member_id'=>$member_id, 'goods_id'=>$goods_id];
        $re = $this->getOne($_data,['id']);
        if($re) $this->baseRemove($re['id']);
        else $this->baseAdd($_data);
        return true;
    }

    /**
     * Notes: 会员中心 - 我的收藏
     * Author: Bruce.z
     * DateTime: 2022/7/18 10:20
     * @param int $member_id
     * @param int $page
     * @param int $size
     * @return array
     */
    public function memberCollect(int $member_id, int $page, int $size):array
    {
        $list = $this->getPageRows(['member_id'=>$member_id],['id','goods_id'], $page, $size);
        if($list['total'] == 0) return $list;

        $good_ids = array_column($list['data'], 'goods_id');
        $list['data'] = GoodsRepository::getInstance()->getRows(['id'=>$good_ids], ['name','img','id','retail_price']);

        return $list;
    }
}
